package cn.icatw.admin.utils;

import cn.icatw.admin.domain.ImageJson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * http客户机
 *
 * @author icatw
 * @date 2022/10/16
 */
@Component
public class HttpClientUtils {

    public static ImageJson temp() throws IOException {
        // 我们需要进行请求的地址：
        String temp = "https://www.dmoe.cc/random.php?return=json";
        try {
            // 1.URL类封装了大量复杂的实现细节，这里将一个字符串构造成一个URL对象
            URL url = new URL(temp);
            // 2.获取HttpURRLConnection对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 3.调用connect方法连接远程资源
            connection.connect();
            // 4.访问资源数据，使用getInputStream方法获取一个输入流用以读取信息
            BufferedReader bReader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), "UTF-8"));

            // 对数据进行访问
            String line = null;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bReader.readLine()) != null) {
                stringBuilder.append(line);
            }

            // 关闭流
            bReader.close();
            // 关闭链接
            connection.disconnect();
            // 打印获取的结果
            System.out.println(stringBuilder.toString());

            // 将获得的String对象转为JSON格式
            JSONObject jsonObject = JSON.parseObject(stringBuilder.toString());
            //JSONObject jsonObject = JSONObject.fromObject(stringBuilder.toString());
            // 通过利用JSON键值对的key，来查询value
            //System.out.println(jsonObject.to);
            System.out.println("code : " + jsonObject.get("code").toString());
            System.out.println("imgurl : " + jsonObject.get("imgurl").toString());
            System.out.println("width : " + jsonObject.get("width").toString());
            System.out.println("height : " + jsonObject.get("height").toString());
            ImageJson imageJson = jsonObject.toJavaObject(ImageJson.class);
            System.out.println(imageJson.toString());
            return imageJson;
            // 对于value也是JSON的对象，将对象的value转为一个新的JSON继续进行
            //JSONObject aObject = JSONObject.fromObject(jsonObject.get("images").toString());
            //System.out.println("images:  " + aObject.get("small").toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    ///**
    // * 有参数请求的get请求
    // *
    // * @param url      请求接口
    // * @param paramMap 请求参数Map对象
    // * @return
    // */
    //public static JSONObject getParamMap(String url, Map<String, String> paramMap) {
    //    CloseableHttpClient httpClient = HttpClients.createDefault();
    //    try {
    //        List<NameValuePair> pairs = new ArrayList<>();
    //        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
    //            pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    //        }
    //        CloseableHttpResponse response;
    //        URIBuilder builder = new URIBuilder(url).setParameters(pairs);
    //        // 执行get请求.
    //        HttpGet httpGet = new HttpGet(builder.build());
    //        response = httpClient.execute(httpGet);
    //        if (response != null && response.getStatusLine().getStatusCode() == 200) {
    //            HttpEntity entity = response.getEntity();
    //            String jsonString = EntityUtils.toString(entity);
    //            JSONObject jsonObject = new JSONObject(jsonString);
    //            //System.out.println("get请求数据成功！\n" + jsonObject);
    //            return jsonObject;
    //        }
    //
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    } finally {
    //        // 关闭连接,释放资源
    //        try {
    //            httpClient.close();
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //    }
    //    return null;
    //}
    //
    //
    ///**
    // * 发送post请求，参数用map接收
    // *
    // * @param url    地址
    // * @param object 请求的对象
    // * @return 返回值
    // */
    //
    //public static JSONObject postMap(String url, Object object) {
    //    //获取json字符串
    //    String json = JSON.toJSONString(object);
    //    System.out.println(json);
    //    CloseableHttpClient httpClient = HttpClients.createDefault();
    //    HttpPost httpPost = new HttpPost(url);
    //    CloseableHttpResponse response;
    //    try {
    //        StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
    //        httpPost.setEntity(stringEntity);
    //        response = httpClient.execute(httpPost);
    //        if (response != null && response.getStatusLine().getStatusCode() == 200) {
    //            HttpEntity entity = response.getEntity();
    //            String jsonString = EntityUtils.toString(entity);
    //            JSONObject jsonObject = new JSONObject(jsonString);
    //            return jsonObject;
    //        }
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    } finally {
    //        // 关闭连接,释放资源
    //        try {
    //            httpClient.close();
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    //    }
    //    return null;
    //}
}

